package com.baidu.ueditor;

import com.alibaba.fastjson.JSONObject;
import com.baidu.ueditor.tools.StringUtils;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

/**
 * 作者 杨柳
 * 创建时间 2017-09-01 16:17
 */
public class UEditorConfig {

    private static String GLOBAL_URL_PREFFIX = "";

    private static String CONFIGP_PATH;

    private static JSONObject CONFIG;

    private static String GLOBAL_SAVE_PATH_PREFFIX = "";

    private static String TOOLBARS;

    public static String DEFAULT_SERVER_URL = "/ueditor";

    static {
        String toolbars = "fullscreen,source,|,undo,redo,|,bold,italic,underline,fontborder,strikethrough,superscript,subscript,removeformat," +
                "formatmatch,autotypeset,blockquote,pasteplain,|,forecolor,backcolor,insertorderedlist,insertunorderedlist,selectall,cleardoc,|," +
                "rowspacingtop,rowspacingbottom,lineheight,|,customstyle,paragraph,fontfamily,fontsize,|,directionalityltr,directionalityrtl," +
                "indent,|,justifyleft,justifycenter,justifyright,justifyjustify,|,touppercase,tolowercase,|,link,unlink,anchor,|," +
                "imagenone,imageleft,imageright,imagecenter,|,simpleupload,insertimage,emotion,scrawl,insertvideo,attachment," +
                "map,insertframe,insertcode,webapp,pagebreak,template,background,|,horizontal,date,time,spechars,snapscreen,wordimage,|," +
                "inserttable,deletetable,insertparagraphbeforetable,insertrow,deleterow,insertcol,deletecol,mergecells,mergeright,mergedown," +
                "splittocells,splittorows,splittocols,charts,|,print,preview,searchreplace,help,drafts";
        init("META-INF/ueditor-default.properties", toolbars);
    }

    public static void setTOOLBARS(String toolbars) {
        UEditorConfig.TOOLBARS = toolbars;
    }

    public void setToolbars(String toolbars) {
        TOOLBARS = toolbars;
    }

    public static String getTOOLBARS() {
        return TOOLBARS;
    }

    private static void init(String config_path, String toolbars) {

        try {
            InputStream is;
            if (config_path.startsWith("/")) {
                is = new FileInputStream(config_path);
            } else {
                is = UEditorConfig.class.getClassLoader().getResourceAsStream(config_path);
            }

            Properties config = new Properties();
            config.load(is);
            createJSONConfig(config);
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
        if (!StringUtils.isEmpty(toolbars)) {
            setTOOLBARS(toolbars);
        }

    }

    public void createConfig(Map<Object, Object> config) {
        createJSONConfig(config);
    }

    public static void createJSONConfig(Map<Object, Object> config) {
        JSONObject object = new JSONObject();
        Set<Object> keys = config.keySet();
        for (Object key : keys) {
            Object value = config.get(key.toString());
            if (key.toString().toLowerCase().contains("urlprefix".toLowerCase())) {
                value = UEditorConfig.getGLOBAL_URL_PREFFIX().concat(value.toString());
            }
            String[] arrays = value.toString().split(",");
            if (arrays.length > 1) {
                object.put(key.toString(), arrays);
            } else {
                object.put(key.toString(), value);
            }
        }
        CONFIG = object;
    }

    public void setGlobalSavePathPreffix(String globalSavePathPreffix) {
        GLOBAL_SAVE_PATH_PREFFIX = globalSavePathPreffix;
    }

    public String getGlobalSavePathPreffix() {
        return GLOBAL_SAVE_PATH_PREFFIX;
    }

    public static String getGLOBAL_SAVE_PATH_PREFFIX() {
        return GLOBAL_SAVE_PATH_PREFFIX;
    }

    public String getGlobalUrlPreffix() {
        return GLOBAL_URL_PREFFIX;
    }

    public void setGlobalUrlPreffix(String globalUrlPreffix) {
        GLOBAL_URL_PREFFIX = globalUrlPreffix;
    }

    public static String getGLOBAL_URL_PREFFIX() {
        return GLOBAL_URL_PREFFIX;
    }

    public String getConfigPath() {
        return CONFIGP_PATH;
    }

    public static String getCONFIGP_PATH() {
        return CONFIGP_PATH;
    }

    public void setConfigPath(String configPath) {
        CONFIGP_PATH = configPath;
        init(configPath, null);
    }

    public static JSONObject getCONFIG() {
        return CONFIG;
    }

}
